import ClassicModel from '../../models/classic.js'
import LikeModel from '../../models/like.js'
let classic = new ClassicModel()
let likeModel = new LikeModel()
Page({
  data: {
    classic: null,
    latest: true,
    first: false,
    latestIndex: 0,
    likeCount: 0,
    likeStatus: false
  },
  // 监听页面加载 http://bl.7yue.pro/v1  ?appkey=98HcsgdJ3mx4Ufcm
  onLoad:function (options){
    classic.getLatest((res)=>{
      this.setData({
        classic: res,
        likeCount: res.fav_nums,
        likeStatus: res.like_status
        // latestIndex: res.index
      })
      // latestClassic currentClassic
    })
  },


  onNext: function(){
    this._updateClassic("next")
  },

  _updateClassic: function(nextOrPrevious){
    let index = this.data.classic.index
    classic.getClassic(index, nextOrPrevious, (res)=>{
      this._getLikeStatus(res.id, res.type)
      this.setData({
        classic: res,
        first: classic.isFirst(res.index),
        latest: classic.isLatest(res.index)
      })
    })
  },

  onPrevious: function(){
    this._updateClassic("previous")
  },

  _getLikeStatus: function(artId, category){
    likeModel.getClassicLikeStatus(artId, category, (res)=>{
      this.setData({
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    })
  },

  onLike: function(e){
    let behavior = e.detail.behavior
    likeModel.like(behavior, this.data.classic.id, this.data.classic.type)
  },
  // 监听页面初次渲染完成
  onReady: function(){

  },
  // 页面显示
  onShow: function(){

  }
})


/*
  const promise = new Promise((resolve, reject)=>{
    wx.request({
      url: 'http://bl.7yue.pro/v1/classic/latest',
      header:{
        appkey: "98HcsgdJ3mx4Ufcm"
      },
      success: res => {
        resolve(res)
      }
    })
  })
  promise.then((res)=>{
    console.log(111)
    return new Promise((resolve,reject)=>{
      setTimeout(()=>{
        console.log(222222)
        resolve()
      },0)
    })
  }).then(()=>{
    console.log(33333)
  })
*/